package sdau.hjk.controller;

import sdau.hjk.entity.User;
import sdau.hjk.service.Impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *@version 1.0
 *@author: huangjiakai
 *@create:2021−02-05 16:43
 *@className:com.hjk.controller.UserController
 *@description: 用户web层
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    @ResponseBody
    public List<User> findAll(){
        List<User> userList = userService.findAll();
        return userList;
    }

    @RequestMapping("/save")
    public void save(User user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        userService.saveUser(user);
    }

    @RequestMapping("/findByOpenid")
    @ResponseBody
    public User findByOpenid(String openid){
        User user = userService.findByOpenid(openid);
        return user;
    }

    @RequestMapping("/findByGrade")
    @ResponseBody
    public List<User> findByGrade(String major,String grade,HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");
        List<User> userList = userService.findByGrade(major,grade);
        return userList;
    }

    @RequestMapping("/findByMajor")
    @ResponseBody
    public List<User> findByMajor(String major){
        return userService.findByMajor(major);
    }

    @RequestMapping("/findById")
    @ResponseBody
    public User findById(Integer id){
        return userService.findById(id);
    }

}
